package problem257;

import java.util.ArrayList;
import java.util.List;

//257.二叉树的所有路径
//https://leetcode.cn/problems/binary-tree-paths/description/

class Solution {
    StringBuffer str = new StringBuffer();
    List<String> list = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root);
        return list;
    }
    public void dfs(TreeNode root) {
        str.append(root.val);
        if(root.left == null && root.right == null) {
            list.add(new String(str));
            return;
        }

        if(root.left != null) {
            str.append("->");
            dfs(root.left);
            str.delete(str.lastIndexOf("->"), str.length());
        }

        if(root.right != null) {
            str.append("->");
            dfs(root.right);
            str.delete(str.lastIndexOf("->"), str.length());
        }
    }
}